import { defineStore } from "pinia";
import axios from "axios";
import { nanoid } from "nanoid";
import { reactive } from "vue";
// 选项式
/*
const useTalkStore = defineStore('talk', {
    actions: {
        async getTalk() {
            let result = await axios.get("https://api.uomg.com/api/rand.qinghua?format=json")
            this.talkList.unshift({ id: nanoid(), content: result.data.content })
        }
    },
    state: () => {
        return {
            talkList: JSON.parse(localStorage.getItem('talkList') as string)||[]//默认最先开始为空的话就赋值为[],不为空localStorage取出来的是字符串将其断言为字符串
        }
    },

})*/
// 组合式 比较常用
const useTalkStore=defineStore('talk',()=>{
    // 这里相当于action
        const talkList=reactive(
            JSON.parse(localStorage.getItem('talkList') as string)||[]
        )
        // 方法可以直接写
        async function getTalk() {
            let result = await axios.get("https://api.uomg.com/api/rand.qinghua?format=json")
            talkList.unshift({ id: nanoid(), content: result.data.content })
       
        }
        return{talkList,getTalk}
})
export default useTalkStore